/* =============================================================================
 * experience, cohort, time decomposition by education groups
 * ===========================================================================*/
 
/*******************************************************************************
									US
*******************************************************************************/

use "data\US.dta", clear

drop if outlier

keep if male == 1

gen wexp = min(age - eduyrs - 6, age - 18) 

rename ybirth byear
drop if byear < 1935
drop if byear > 1984

rename lnearnings lwage
rename asecwt perwt 

/* 
education groups:
073	High school diploma or equivalent (before 1991 there are large amount of workers with 072 12th grade, diploma unclear)
111	Bachelor's degree (before 1991 only have 110 4 years of college)
*/

gen edug = .
replace edug = 0 if educ == 73  | educ == 72
replace edug = 1 if educ == 111 | educ == 110


forvalues edu = 0/1{

preserve

keep if edug == `edu'

do "code\HLT.do"

keep profile_wexp* plot_wexp profile_coh* plot_coh profile_year* plot_year

foreach v of varlist profile_wexp* profile_coh* profile_year* {
	rename `v' `v'_us
}

save "temp\us_profile_HLT_edu`edu'.dta", replace

restore
}


forvalues edu = 0/1{ 
use "temp\us_profile_HLT_edu`edu'.dta",clear
drop if profile_wexp_us == .
keep profile_wexp* plot_wexp
rename profile_wexp_us profile_wexp_us_`edu'
save "temp\us_exp_profile_edu`edu'.dta",replace

use "temp\us_profile_HLT_edu`edu'.dta",clear
drop if profile_coh_us == .
keep profile_coh* plot_coh
rename profile_coh_us profile_coh_us_`edu'
save "temp\us_coh_profile_edu`edu'.dta",replace

use "temp\us_profile_HLT_edu`edu'.dta",clear
keep profile_year* plot_year
rename profile_year_us profile_year_us_`edu'
save "temp\us_year_profile_edu`edu'.dta",replace
}



/*******************************************************************************
									China
*******************************************************************************/

use "data\China.dta", clear

drop if outlier

keep if male == 1

gen wexp = min(age - eduyrs - 6, age - 18) 

rename ybirth byear
drop if byear < 1935
drop if byear > 1984

gen perwt = 1

gen edug = .
replace edug = 0 if eduyrs == 12
replace edug = 1 if eduyrs == 16

rename lnearnings lwage

forvalues edu = 1(-1)0 {

preserve

keep if edug == `edu'

do "code\HLT.do"

keep profile_wexp* plot_wexp profile_coh* plot_coh profile_year* plot_year

foreach v of varlist profile_wexp* profile_coh* profile_year* {
	rename `v' `v'_cn
}

save "temp\cn_profile_HLT_edu`edu'.dta", replace

restore
}

forvalues edu = 0/1{ 
use "temp\cn_profile_HLT_edu`edu'.dta",clear
keep if profile_wexp_cn ~= .
keep profile_wexp* plot_wexp
rename profile_wexp_cn profile_wexp_cn_`edu'
save "temp\cn_exp_profile_edu`edu'.dta",replace

use "temp\cn_profile_HLT_edu`edu'.dta",clear
keep if profile_coh_cn ~= .
keep profile_coh* plot_coh
rename profile_coh_cn profile_coh_cn_`edu'
save "temp\cn_coh_profile_edu`edu'.dta",replace

use "temp\cn_profile_HLT_edu`edu'.dta",clear
keep profile_year* plot_year
rename profile_year_cn profile_year_cn_`edu'
save "temp\cn_year_profile_edu`edu'.dta",replace
}




/*******************************************************************************

										Graphs
		
*******************************************************************************/

*** Experience ***
use "temp\cn_exp_profile_edu0.dta", clear
merge 1:1 plot_wexp using "temp\cn_exp_profile_edu1.dta"
drop _merge
merge 1:1 plot_wexp using "temp\us_exp_profile_edu0.dta"
drop _merge
merge 1:1 plot_wexp using "temp\us_exp_profile_edu1.dta"
drop _merge

replace plot_wexp = plot_wexp + 2

twoway	(scatter profile_wexp_us_0 plot_wexp, msymbol(dh) mcolor(blue) msize(medlarge) connect(l) lpattern(dash) lcolor(blue))	///
	(scatter profile_wexp_us_1 plot_wexp, msymbol(d) mcolor(blue) msize(medlarge) connect(l) lpattern(solid) lcolor(blue))		/// 
	(scatter profile_wexp_cn_0 plot_wexp, msymbol(oh) mcolor(red) msize(medlarge) connect(l) lpattern(dash) lcolor(red))	///
	(scatter profile_wexp_cn_1 plot_wexp, msymbol(o) mcolor(red) msize(medlarge) connect(l) lpattern(solid) lcolor(red)),		///
	xlabel(0(5)40,labsize(large)) ylabel(1(1)4,labsize(large))  	///
	xtitle("Potential Experience",size(large)) ytitle("")	///
	legend(order(1 "US High-School" 2 "US College" 3 "China High-School" 4 "China College") rows(1) pos(6) size(medium))	///
	title("Experience Effects",size(large) color(black)) name(exp, replace)

*** Cohort ***
use "temp\cn_coh_profile_edu0.dta", clear
merge 1:1 plot_coh using "temp\cn_coh_profile_edu1.dta"
drop _merge
merge 1:1 plot_coh using "temp\us_coh_profile_edu0.dta"
drop _merge
merge 1:1 plot_coh using "temp\us_coh_profile_edu1.dta"
drop _merge

replace plot_coh = plot_coh + 2

twoway 	(scatter profile_coh_us_0 plot_coh, msymbol(dh) mcolor(blue) msize(medlarge) connect(l) lpattern(dash) lcolor(blue))	///
	(scatter profile_coh_us_1 plot_coh, msymbol(d) mcolor(blue) msize(medlarge) connect(l) lpattern(solid) lcolor(blue))		/// 
	(scatter profile_coh_cn_0 plot_coh, msymbol(oh) mcolor(red) msize(medlarge) connect(l) lpattern(dash) lcolor(red))	///
	(scatter profile_coh_cn_1 plot_coh, msymbol(o) mcolor(red) msize(medlarge) connect(l) lpattern(solid) lcolor(red)),		///
	xlabel(1935(10)1985,labsize(large))  xmtick(##2) ylabel(1(0.25)2,labsize(large))		///
	xtitle("Birth Year",size(large)) ytitle("")	///
	legend(order(1 "US High-School" 2 "US College" 3 "China High-School" 4 "China College") rows(1) pos(6) size(medium))	///
	title("Cohort Effects",size(large) color(black)) name(coh, replace)	
	
*** Time ***
use "temp\cn_year_profile_edu0.dta", clear
merge 1:1 plot_year using "temp\cn_year_profile_edu1.dta"
drop _merge
merge 1:1 plot_year using "temp\us_year_profile_edu0.dta"
drop _merge
merge 1:1 plot_year using "temp\us_year_profile_edu1.dta"
drop _merge

foreach country in cn us {
	forvalues edu = 0/1{
		gen normalize_`country'_`edu' = profile_year_`country'_`edu'[1]
		replace profile_year_`country'_`edu' = profile_year_`country'_`edu' / normalize_`country'_`edu'
	}
}

twoway 	(scatter profile_year_us_0 plot_year, msymbol(dh) mcolor(blue) msize(medlarge) connect(l) lpattern(dash) lcolor(blue))	///
	(scatter profile_year_us_1 plot_year, msymbol(d) mcolor(blue) msize(medlarge) connect(l) lpattern(solid) lcolor(blue))		/// 
	(scatter profile_year_cn_0 plot_year, msymbol(oh) mcolor(red) msize(medlarge) connect(l) lpattern(dash) lcolor(red))	///
	(scatter profile_year_cn_1 plot_year, msymbol(o) mcolor(red) msize(medlarge) connect(l) lpattern(solid) lcolor(red)),		///
	xlabel(1985(5)2010,labsize(large)) ylabel(1(1)5,labsize(large)) 	///
	xtitle("Year",size(large)) ytitle("")	///
	legend(order(1 "US High-School" 2 "US College" 3 "China High-School" 4 "China College") rows(1) pos(6) size(medium))	///
	title("Time Effects",size(large) color(black)) name(year, replace)


grc1leg exp coh year, name(combine, replace) row(1)

graph display combine, xsize(20) ysize(8)

graph export "figures\decomposition_by_edu.pdf", as(pdf) replace	
